home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ListPanel.java < prev    next >
Text File  |  1998-06-30  |  16KB  |  448 lines

  1. /*
  2.  * @(#)ListPanel.java    1.5 98/01/31
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.text.*;
  23. import com.sun.java.swing.plaf.basic.BasicListCellRenderer;
  24.  
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import java.util.*;
  28.  
  29.  
  30. /**
  31.  * ListBox!
  32.  *
  33.  * @version 1.5 01/31/98
  34.  * @author Jeff Dinkins
  35.  */
  36. public class ListPanel extends JPanel 
  37. {
  38.     int fastfoodIndex;
  39.     int dessertIndex;
  40.     int fruitIndex;
  41.     int veggieIndex;
  42.  
  43.     boolean fastfoodShown;
  44.     boolean dessertShown;
  45.     boolean fruitShown;
  46.     boolean veggieShown;
  47.  
  48.     // Fast Food
  49.     public ImageIcon burger    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/burger.gif","burger");
  50.     public ImageIcon fries     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/fries.gif","fries");
  51.     public ImageIcon softdrink = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/softdrink.gif","soft drink");
  52.     public ImageIcon hotdog    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/hotdog.gif","hot dog");
  53.     public ImageIcon pizza     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/pizza.gif","pizza");
  54.  
  55.     // Dessert
  56.     public ImageIcon icecream = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/icecream.gif","ice cream");
  57.     public ImageIcon pie      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/pie.gif","pie");
  58.     public ImageIcon cake     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/cake.gif","cake");
  59.     public ImageIcon donut    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/donut.gif","donut");
  60.     public ImageIcon treat    = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/treat.gif","treat");
  61.  
  62.     // Fruit
  63.     public ImageIcon grapes      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/grapes.gif","grapes");
  64.     public ImageIcon banana      = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/banana.gif","banana");
  65.     public ImageIcon watermelon  = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/watermelon.gif","watermelon");
  66.     public ImageIcon cantaloupe  = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/cantaloupe.gif","cantaloupe");
  67.     public ImageIcon peach       = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/peach.gif","peach");
  68.  
  69.     // Veggie
  70.     public ImageIcon broccoli = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/broccoli.gif","broccoli");
  71.     public ImageIcon carrot   = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/carrot.gif","carrot");
  72.     public ImageIcon peas     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/peas.gif","peas");
  73.     public ImageIcon corn     = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/corn.gif","corn");
  74.     public ImageIcon radish   = SwingSet.sharedInstance().loadImageIcon("images/ImageClub/food/radish.gif","radish");
  75.  
  76.  
  77.     // The Frame
  78.     SwingSet swing;
  79.     JList listBox;
  80.     JScrollPane scrollPane;
  81.  
  82.     DefaultListModel model = new DefaultListModel();
  83.     JLabel priceLabel;
  84.     int listPrice = 0;
  85.  
  86.     JButton reset;
  87.     JButton purchase;
  88.  
  89.     JRadioButton dessertRadioButton;
  90.     JRadioButton veggieRadioButton;
  91.     JRadioButton fruitRadioButton;
  92.     JRadioButton fastfoodRadioButton;
  93.  
  94.     JCheckBox dessertCheckbox;
  95.     JCheckBox veggieCheckbox;
  96.     JCheckBox fruitCheckbox;
  97.     JCheckBox fastfoodCheckbox;
  98.  
  99.     public ListPanel(SwingSet swing) {
  100.     this.swing = swing;
  101.  
  102.     setBorder(swing.emptyBorder5);
  103.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  104.  
  105.     // create the list
  106.     for(int i = 0; i < ITEMS; i++) {
  107.             model.addElement(new Integer(i));
  108.         }
  109.  
  110.     listBox = new JList(model) {
  111.         public Dimension getMaximumSize() {
  112.         return new Dimension(400, super.getMaximumSize().height);
  113.         }
  114.     };
  115.         listBox.setCellRenderer(new TestCellRenderer(listBox));
  116.  
  117.     // Create the controls 
  118.     JPanel controlPanel = new JPanel() {
  119.         public Dimension getMaximumSize() {
  120.         return new Dimension(300, super.getMaximumSize().height);
  121.         }
  122.     };
  123.     controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
  124.     controlPanel.setBorder(swing.loweredBorder);
  125.     controlPanel.setAlignmentY(TOP_ALIGNMENT);
  126.  
  127.     // List operations
  128.  
  129.     JPanel pricePanel = swing.createHorizontalPanel(false);
  130.     pricePanel.setAlignmentY(TOP_ALIGNMENT);
  131.     pricePanel.setAlignmentX(LEFT_ALIGNMENT);
  132.     controlPanel.add(pricePanel);
  133.     purchase = new JButton("Purchase");
  134.     purchase.setToolTipText("Adds the selected item(s) to your grocery bill.");
  135.     pricePanel.add(purchase);
  136.     pricePanel.add(Box.createRigidArea(swing.hpad10));
  137.  
  138.     priceLabel = new JLabel("Total:                           ");
  139.     pricePanel.add(priceLabel);
  140.  
  141.     controlPanel.add(Box.createRigidArea(swing.vpad20));
  142.     JLabel l = new JLabel("Jump To:");
  143.     l.setFont(swing.boldFont);
  144.     controlPanel.add(l);
  145.     ButtonGroup group = new ButtonGroup();
  146.  
  147.     fastfoodRadioButton = new JRadioButton("Fast Food");
  148.     fastfoodRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  149.     group.add(fastfoodRadioButton);
  150.     controlPanel.add(fastfoodRadioButton);
  151.  
  152.     dessertRadioButton = new JRadioButton("Desserts");
  153.     dessertRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  154.     group.add(dessertRadioButton);
  155.     controlPanel.add(dessertRadioButton);
  156.  
  157.     fruitRadioButton = new JRadioButton("Fruits");
  158.     fruitRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
  159.     group.add(fruitRadioButton);
  160.     controlPanel.add(fruitRadioButton);
  161.  
  162.     veggieRadioButton = new JRadioButton("Vegetables");
  163.     veggieRadioButton.setToolTipText("Calls list.ensureVisible(index) to jump to the items.");
  164.     group.add(veggieRadioButton);
  165.     controlPanel.add(veggieRadioButton);
  166.  
  167.     controlPanel.add(Box.createRigidArea(swing.vpad20));
  168.     l = new JLabel("Show:");
  169.     l.setFont(swing.boldFont);
  170.     controlPanel.add(l);
  171.     fastfoodCheckbox = new JCheckBox("Fast Food");
  172.     fastfoodCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  173.     fastfoodCheckbox.setSelected(true);
  174.     controlPanel.add(fastfoodCheckbox);
  175.  
  176.     dessertCheckbox = new JCheckBox("Desserts");
  177.     dessertCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  178.     dessertCheckbox.setSelected(true);
  179.     controlPanel.add(dessertCheckbox);
  180.  
  181.     fruitCheckbox = new JCheckBox("Fruits");
  182.     fruitCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  183.     fruitCheckbox.setSelected(true);
  184.     controlPanel.add(fruitCheckbox);
  185.  
  186.     veggieCheckbox = new JCheckBox("Vegetables");
  187.     veggieCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
  188.     veggieCheckbox.setSelected(true);
  189.     controlPanel.add(veggieCheckbox);
  190.  
  191.     controlPanel.add(Box.createGlue());
  192.     reset = new JButton("Reset");
  193.     reset.setToolTipText("Resets the state of the demo.");
  194.     controlPanel.add(reset);
  195.  
  196.  
  197.     scrollPane = new JScrollPane(listBox);
  198.     scrollPane.setAlignmentX(LEFT_ALIGNMENT);
  199.     scrollPane.setAlignmentY(TOP_ALIGNMENT);
  200.     add(scrollPane);
  201.     add(Box.createRigidArea(swing.hpad10));
  202.      add(controlPanel);
  203.  
  204.     ActionListener purchaseListener = new ActionListener() {
  205.             public void actionPerformed(ActionEvent e) {
  206.         int first = listBox.getMinSelectionIndex();
  207.                 int last = listBox.getMaxSelectionIndex();
  208.         if(first < 0) {
  209.             return;
  210.         }
  211.         for(int i = first; i <= last; i++) {
  212.             Integer item = (Integer) model.getElementAt(i);
  213.             listPrice += price[item.intValue()];
  214.         }
  215.         priceLabel.setText("Total: $" + ((double) listPrice)/100.0);
  216.         priceLabel.repaint();
  217.         }
  218.     };
  219.     purchase.addActionListener(purchaseListener);
  220.  
  221.     ActionListener showListener = new ActionListener() {
  222.             public void actionPerformed(ActionEvent e) {
  223.         JCheckBox cb = (JCheckBox) e.getSource();
  224.         String label = cb.getText();
  225.         if(!cb.isSelected()) {
  226.  
  227.             if(label.equals("Fast Food")) {
  228.             fastfoodShown = false;
  229.             for(int i = fastfoodIndex; i < fastfoodIndex+5; i++) {
  230.                 model.removeElementAt(fastfoodIndex);
  231.             }
  232.             fastfoodRadioButton.setEnabled(false);
  233.             dessertIndex -= 5;
  234.             fruitIndex -= 5;
  235.             veggieIndex -= 5;
  236.             scrollPane.validate();
  237.             } else if(label.equals("Desserts")) {
  238.             for(int i = dessertIndex; i < dessertIndex+5; i++) {
  239.                 model.removeElementAt(dessertIndex);
  240.             }
  241.             dessertRadioButton.setEnabled(false);
  242.             fruitIndex -= 5;
  243.             veggieIndex -= 5;
  244.             scrollPane.validate();
  245.             } else if(label.equals("Fruits")) {
  246.             for(int i = fruitIndex; i < fruitIndex+5; i++) {
  247.                 model.removeElementAt(fruitIndex);
  248.             }
  249.             fruitRadioButton.setEnabled(false);
  250.             veggieIndex -= 5;
  251.             scrollPane.validate();
  252.             } else if(label.equals("Vegetables")) {
  253.             for(int i = veggieIndex; i < veggieIndex+5; i++) {
  254.                 model.removeElementAt(veggieIndex);
  255.             }
  256.             veggieRadioButton.setEnabled(false);
  257.             scrollPane.validate();
  258.             }
  259.             if(model.getSize() < 1)
  260.                 listBox.getParent().repaint();
  261.         } else {
  262.             if(label.equals("Fast Food")) {
  263.             model.insertElementAt(new Integer(4), 0);
  264.             model.insertElementAt(new Integer(3), 0);
  265.             model.insertElementAt(new Integer(2), 0);
  266.             model.insertElementAt(new Integer(1), 0);
  267.             model.insertElementAt(new Integer(0), 0);
  268.             dessertIndex += 5;
  269.             fruitIndex += 5;
  270.             veggieIndex += 5;
  271.             fastfoodRadioButton.setEnabled(true);
  272.             scrollPane.validate();
  273.             } else if(label.equals("Desserts")) {
  274.             model.insertElementAt(new Integer(9), dessertIndex);
  275.             model.insertElementAt(new Integer(8), dessertIndex);
  276.             model.insertElementAt(new Integer(7), dessertIndex);
  277.             model.insertElementAt(new Integer(6), dessertIndex);
  278.             model.insertElementAt(new Integer(5), dessertIndex);
  279.             fruitIndex += 5;
  280.             veggieIndex += 5;
  281.             dessertRadioButton.setEnabled(true);
  282.             scrollPane.validate();
  283.             } else if(label.equals("Fruits")) {
  284.             model.insertElementAt(new Integer(14), fruitIndex);
  285.             model.insertElementAt(new Integer(13), fruitIndex);
  286.             model.insertElementAt(new Integer(12), fruitIndex);
  287.             model.insertElementAt(new Integer(11), fruitIndex);
  288.             model.insertElementAt(new Integer(10), fruitIndex);
  289.             veggieIndex += 5;
  290.             fruitRadioButton.setEnabled(true);
  291.             scrollPane.validate();
  292.             } else if(label.equals("Vegetables")) {
  293.             model.insertElementAt(new Integer(19), veggieIndex);
  294.             model.insertElementAt(new Integer(18), veggieIndex);
  295.             model.insertElementAt(new Integer(17), veggieIndex);
  296.             model.insertElementAt(new Integer(16), veggieIndex);
  297.             model.insertElementAt(new Integer(15), veggieIndex);
  298.             veggieRadioButton.setEnabled(true);
  299.             scrollPane.validate();
  300.             }
  301.         }
  302.         }
  303.     };
  304.     fruitCheckbox.addActionListener(showListener);
  305.     veggieCheckbox.addActionListener(showListener);
  306.     dessertCheckbox.addActionListener(showListener);
  307.     fastfoodCheckbox.addActionListener(showListener);
  308.  
  309.     ActionListener jumpListener = new ActionListener() {
  310.             public void actionPerformed(ActionEvent e) {
  311.         JRadioButton rb = (JRadioButton) e.getSource();
  312.         if(rb.isSelected()) {
  313.             String label = rb.getText();
  314.             if(label.equals("Fruits")) {
  315.             listBox.ensureIndexIsVisible(fruitIndex+5);
  316.             listBox.ensureIndexIsVisible(fruitIndex);
  317.             } else if(label.equals("Desserts")) {
  318.             listBox.ensureIndexIsVisible(dessertIndex+5);
  319.             listBox.ensureIndexIsVisible(dessertIndex);
  320.             } else if(label.equals("Vegetables")) {
  321.             listBox.ensureIndexIsVisible(veggieIndex+5);
  322.             listBox.ensureIndexIsVisible(veggieIndex);
  323.             } else if(label.equals("Fast Food")) {
  324.             listBox.ensureIndexIsVisible(fastfoodIndex+5);
  325.             listBox.ensureIndexIsVisible(fastfoodIndex);
  326.             }
  327.         }
  328.         }
  329.     };
  330.     fruitRadioButton.addActionListener(jumpListener);
  331.     veggieRadioButton.addActionListener(jumpListener);
  332.     dessertRadioButton.addActionListener(jumpListener);
  333.     fastfoodRadioButton.addActionListener(jumpListener);
  334.  
  335.     ActionListener resetListener = new ActionListener() {
  336.             public void actionPerformed(ActionEvent e) {
  337.         resetAll();
  338.             }
  339.     };
  340.     reset.addActionListener(resetListener);
  341.     }
  342.  
  343.     public void resetAll() {
  344.     model.removeAllElements();
  345.     
  346.     fastfoodCheckbox.setSelected(true);
  347.     fruitCheckbox.setSelected(true);
  348.     veggieCheckbox.setSelected(true);
  349.     dessertCheckbox.setSelected(true);
  350.  
  351.     fastfoodRadioButton.setEnabled(true);
  352.     fruitRadioButton.setEnabled(true);
  353.     veggieRadioButton.setEnabled(true);
  354.     dessertRadioButton.setEnabled(true);
  355.  
  356.     fastfoodRadioButton.setSelected(true);
  357.     
  358.     for(int i = 0; i < ITEMS; i++) {
  359.         model.addElement(new Integer(i));
  360.     }
  361.  
  362.     fastfoodShown = true;
  363.     dessertShown  = true;
  364.     fruitShown    = true;
  365.     veggieShown   = true;
  366.  
  367.     fastfoodIndex = 0;
  368.     dessertIndex  = 5;
  369.     fruitIndex    = 10;
  370.     veggieIndex   = 15;
  371.  
  372.     listPrice = 0;
  373.     
  374.     priceLabel.setText("Total:  $0.00   ");
  375.     listBox.ensureIndexIsVisible(fastfoodIndex);
  376.  
  377.     scrollPane.validate();
  378.     }
  379.  
  380.     static int ITEMS = 20;
  381.     ImageIcon images[];
  382.     String     desc[];
  383.     int        price[];
  384.  
  385.     class TestCellRenderer extends BasicListCellRenderer
  386.     {
  387.     TestCellRenderer(JList listBox) {
  388.         super();
  389.  
  390.         images = new ImageIcon[ITEMS];
  391.         desc = new String[ITEMS];
  392.         price = new int[ITEMS];
  393.  
  394.         int i = 0;
  395.         // 5 - FastFood
  396.         images[i] = burger;      price[i] = 199;   desc[i++] = "Burger";
  397.         images[i] = fries;       price[i] = 99;    desc[i++] = "Fries";
  398.         images[i] = softdrink;   price[i] = 89;    desc[i++] = "Cola";
  399.         images[i] = pizza;       price[i] = 399;   desc[i++] = "Pizza";
  400.         images[i] = hotdog;      price[i] = 299;   desc[i++] = "Hotdog";
  401.  
  402.         // 10 - Dessert
  403.         images[i] = icecream;    price[i] = 199;   desc[i++] = "Ice Cream";
  404.         images[i] = pie;         price[i] = 249;   desc[i++] = "Cherry Pie";
  405.         images[i] = cake;        price[i] = 355;   desc[i++] = "Cake";
  406.         images[i] = donut;       price[i] = 25;    desc[i++] = "Donut";
  407.         images[i] = treat;       price[i] = 52;    desc[i++] = "Fruit Pop";
  408.  
  409.         // 15 Fruit
  410.         images[i] = grapes;      price[i] = 99;    desc[i++] = "Grapes";
  411.         images[i] = watermelon;  price[i] = 59;    desc[i++] = "Watermelon";
  412.         images[i] = peach;       price[i] = 35;    desc[i++] = "Peach";
  413.         images[i] = cantaloupe;  price[i] = 85;    desc[i++] = "Cantaloupe";
  414.         images[i] = banana;      price[i] = 25;    desc[i++] = "Banana";
  415.  
  416.         // 20 - Veggies
  417.         images[i] = broccoli;    price[i] = 99;    desc[i++] = "Broccoli";
  418.         images[i] = corn;        price[i] = 65;    desc[i++] = "Corn";
  419.         images[i] = carrot;      price[i] = 25;    desc[i++] = "Carrot";
  420.         images[i] = peas;        price[i] =  3;    desc[i++] = "Peas";
  421.         images[i] = radish;      price[i] = 45;    desc[i++] = "Radish";
  422.     }
  423.  
  424.  
  425.     public Component getListCellRendererComponent(
  426.             JList list, 
  427.             Object value, 
  428.             int modelIndex, 
  429.             boolean isSelected, 
  430.             boolean cellHasFocus) 
  431.         {
  432.         int index = ((Integer)value).intValue();
  433.         String text;
  434.         if(isSelected) {
  435.         text = "  " + desc[index] + "    $" + ((double)price[index])/100.0;
  436.         } 
  437.         else {
  438.         text = "  " + desc[index];
  439.         }
  440.         setIcon(images[index]);
  441.  
  442.         return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
  443.     }
  444.     }
  445. }
  446.  
  447.  
  448.